<?php

namespace yan\backendclient\modules\role\controllers;

use Yii;
use yan\backendclient\modules\role\models\BackendRoleItem;
use yii\data\ActiveDataProvider;
use yan\backendclient\controllers\DebugController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yan\backendclient\modules\role\models\Rbac;
/**
 * ItemController implements the CRUD actions for BackendRoleItem model.
 */
class ItemController extends DebugController
{
    

    /**
     * Lists all BackendRoleItem models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => BackendRoleItem::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single BackendRoleItem model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new BackendRoleItem model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new BackendRoleItem();
        $model->setScenario('permission');

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if( $model->createPermission() ){
                return $this->redirect(['index']);
            }
            
        } 
        return $this->render('create', [
            'model' => $model,
        ]);
        
    }

    /**
     * Updates an existing BackendRoleItem model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        $model->fillModel();
        $model->setScenario('permission');
        $name = $model->name;
        
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            if( $model->updatePermission($name) )
            return $this->redirect(['index']);
        }

        return $this->render('update', [
                'model' => $model,
            ]);
    }

    /**
     * Deletes an existing BackendRoleItem model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the BackendRoleItem model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return BackendRoleItem the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = BackendRoleItem::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
